﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace myCommon
{
    // ————————————————
    // 版权声明：本文为CSDN博主「sunny906」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
    // 原文链接：https://blog.csdn.net/sunny906/article/details/40865073
    public static class WindowControlHelper
    {
        public static void PrintVisualTree(DependencyObject obj, int level, StringBuilder tree)
        {
            tree.AppendLine(new string(' ', level) + obj);
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                PrintVisualTree(VisualTreeHelper.GetChild(obj, i), level + 1, tree);
            }
        }

        public static void PrintLogicalTree(DependencyObject obj, int level, StringBuilder tree)
        {
            tree.AppendLine(new string(' ', level) + obj);
            foreach (var v in LogicalTreeHelper.GetChildren(obj))
            {
                if (v is DependencyObject)
                {
                    PrintLogicalTree(v as DependencyObject, level + 1, tree);
                }
            }
        }

        public static void GetVisualTreeChildrenHash(DependencyObject obj, int level, List<int> HashList)
        {
            HashList.Add(obj.GetHashCode());
            
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                GetVisualTreeChildrenHash(VisualTreeHelper.GetChild(obj, i), level + 1, HashList);
            }
        }
        public static void GetLogicalTreeChildrenHash(DependencyObject obj, int level, List<int>HashList)
        {
            HashList.Add(obj.GetHashCode());
            foreach (var v in LogicalTreeHelper.GetChildren(obj))
            {
                if (v is DependencyObject)
                {
                    GetLogicalTreeChildrenHash(v as DependencyObject, level + 1, HashList);
                }
            }
        }

        public static bool CheckItemInOjb(DependencyObject obj, int level, int hashCode)
        {
            //HashList.Add(obj.GetHashCode());

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                var obj_currnet = VisualTreeHelper.GetChild(obj, i);
                if (obj_currnet.GetHashCode() == hashCode) return true;
                CheckItemInOjb(obj_currnet, level + 1, hashCode);
            }
            return false;
        }

    }
}
